// pages/reward/index.js
const App = getApp()
const { API, Ubt, imageHash, User } = App.services
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radioItems: [
      {name: '单店免单', value: '0'},
      {name: '全店免单', value: '1'}
    ],
    checkboxItems: [
      {name: '周一', value: '1'},
      {name: '周二', value: '2'},
      {name: '周三', value: '3'},
      {name: '周四', value: '4'},
      {name: '周五', value: '5'},
      {name: '周六', value: '6'},
      {name: '周日', value: '7'}
    ],
    imageDtos:[],
    weekDays:[],
    functionName:'',
    functionRemark:'',
    functionType:0,
    maxCount:0,
    shopId:0,
    shopFunctionImageDtos:[],
    rate0Count:0,//免单数量
    rate1Count:0,//1折数量
    rate2Count:0,//2折数量
    rate3Count:0,//3折数量
    rate4Count:0,//4折数量
    rate5Count:0,//5折数量
    rate6Count:0,//6折数量
    rate7Count:0,//7折数量
    rate8Count:0,//8折数量
    rate9Count:0,//9折数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  radioChange:function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
    }
    this.setData({
      radioItems: radioItems
    });
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);

    var checkboxItems = this.data.checkboxItems, values = e.detail.value;
    for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
      checkboxItems[i].checked = false;

      for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
        if(checkboxItems[i].value == values[j]){
          checkboxItems[i].checked = true;
          break;
        }
      }
    }

    this.setData({
      checkboxItems: checkboxItems
    });
  },
  bindRewardName:function (e) {
    this.setData({ functionName: e.detail.value })
  },
  bindRewardRemark:function (e) {
    this.setData({ functionRemark: e.detail.value })
  },
  bindMaxCount:function (e) {
    this.setData({ maxCount: e.detail.value })
  },
  chooseImage: function(e) {
    var that = this;
    var type =  e.currentTarget.dataset.type
    //选择并上传图片--Max：9张
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],

      success: function (res) {
        //将图片路径循环赋值给filePath参数
        that.setData({
          imageDtos:[]
        })
        for (var i = 0; i < res.tempFilePaths.length; i++) {

          var imgUrl = res.tempFilePaths[i];

          wx.uploadFile({
            //上传图片的网路请求地址
            url: 'http://localhost:9002/web_api/v2/file/uploadRatio',
            //选择
            filePath: imgUrl,
            name: 'file',
            success: function (resp) {
              var imageDtos = that.data.imageDtos
              imageDtos.push(resp.data)
              that.setData({
                imageDtos:imageDtos
              })
            },

            fail: function (res) {
              console.log("error");
            }
          });

        }//for循环结束

      }
    });
  },
  bindRate0Count:function (e) {
    this.setData({
      rate0Count:e.detail.value
    })
  },
  bindRate1Count:function (e) {
    this.setData({
      rate1Count:e.detail.value
    })
  },
  bindRate2Count:function (e) {
    this.setData({
      rate2Count:e.detail.value
    })
  },
  bindRate3Count:function (e) {
    this.setData({
      rate3Count:e.detail.value
    })
  },
  bindRate4Count:function (e) {
    this.setData({
      rate4Count:e.detail.value
    })
  },
  bindRate5Count:function (e) {
    this.setData({
      rate5Count:e.detail.value
    })
  },
  bindRate6Count:function (e) {
    this.setData({
      rate6Count:e.detail.value
    })
  },
  bindRate7Count:function (e) {
    this.setData({
      rate7Count:e.detail.value
    })
  },
  bindRate8Count:function (e) {
    this.setData({
      rate8Count:e.detail.value
    })
  },
  bindRate9Count:function (e) {
    this.setData({
      rate9Count:e.detail.value
    })
  },
  regiestFunction:function(){
    if(!this.data.functionName){
      wx.showModal({
        title:'温馨提示',
        content:'请输入活动名称',
        showCancel:false
      })
      return
    }
    if(!this.data.functionRemark){
      wx.showModal({
        title:'温馨提示',
        content:'请输入活动描述',
        showCancel:false
      })
      return
    }
    if(!this.data.maxCount){
      wx.showModal({
        title:'温馨提示',
        content:'请输入发放券的总数量',
        showCancel:false
      })
      return
    }
    if(this.data.imageDtos.length<4){
      wx.showModal({
        title:'温馨提示',
        content:'请至少上传4张活动图片',
        showCancel:false
      })
      return
    }
    if(!this.data.rate0Count){
      wx.showModal({
        title:'温馨提示',
        content:'免单数量必须大于0',
        showCancel:false
      })
      return
    }


    for(var obj of this.data.radioItems){
      if(obj.checked){
        this.setData({
          functionType:obj.value
        })
      }
    }
    if(!this.data.functionType){
      wx.showModal({
        title:'温馨提示',
        content:'请选择活动类型',
        showCancel:false
      })
      return
    }
    var weekDays = []
    for(var obj of this.data.checkboxItems){
      if(obj.checked){
        weekDays.push(obj.value)
      }
    }
    this.setData({
      weekDays:weekDays
    })
    if(this.data.weekDays.length==0){
      wx.showModal({
        title:'温馨提示',
        content:'请选择活动时间',
        showCancel:false
      })
      return
    }
    var user = wx.getStorageSync('USER')
    var data={
      functionName:this.data.functionName,
      functionRemark:this.data.functionRemark,
      maxCount:this.data.maxCount,
      rate0Count:this.data.rate0Count,
      rate1Count:this.data.rate1Count,
      rate2Count:this.data.rate2Count,
      rate3Count:this.data.rate3Count,
      rate4Count:this.data.rate4Count,
      rate5Count:this.data.rate5Count,
      rate6Count:this.data.rate6Count,
      rate7Count:this.data.rate7Count,
      rate8Count:this.data.rate8Count,
      rate9Count:this.data.rate9Count,
      functionType:this.data.functionType,
      weekDays:weekDays,
      userId: user.userId,
      imageDtos:this.data.imageDtos

    }
    console.info(JSON.stringify(data))

    API.createRewardFunction(data).then(res=>{

    })
  },

})
